package com.zzuli.dcxt.controller;

import com.zzuli.dcxt.entiy.Category;
import com.zzuli.dcxt.entiy.Dishes;
import com.zzuli.dcxt.service.DishesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/dishes")
public class DisheseController {
    @Autowired
    private DishesService dishesService;
    //查询所有的菜单
    @RequestMapping("/select")
    public String selectAll(HttpSession session){

        List<Dishes> dishes = dishesService.selectAll();
        session.setAttribute("dishes",dishes);
        return "admin/booklist";
    }
    @RequestMapping("/add")
    public String addDisehs(Dishes dishes){
       dishesService.addDishes(dishes);
        return "forward:/dishes/select.do";
    }
    @RequestMapping("/selectOne")
    public String selectOne( int id, HttpSession session){
     Dishes dishes= dishesService.selectOne(id);
     session.setAttribute("updateDishe",dishes);
     return "/admin/update";
    }

    @RequestMapping("/update")
    public String update(Dishes dishes){
        System.out.println(dishes);
        dishesService.update(dishes);
        return "forward:/dishes/select.do";
    }
    @RequestMapping("/delete")
    public String delete(int id){
        dishesService.delete(id);
        return "forward:/dishes/select.do";
    }
    @RequestMapping("/get")
    public String getCategory(Model model){
    List<Category> typeList= dishesService.getCategory();
    model.addAttribute("typeList",typeList);
        System.out.println(typeList);
    return "admin/addDishes";
    }

    @RequestMapping(value = "/searchDishes",method = RequestMethod.POST,produces="text/html;charset=utf-8")
    @ResponseBody
    public String searchLbByKeyword(String keyword) {
        String strs = dishesService.searchLbByKeyword(keyword);
        System.out.println(strs);
        return strs;
    }
    @RequestMapping("/test")
    @ResponseBody
    public String test1(HttpServletResponse response){
        response.setContentType("text/html;charset=utf-8");
        return "你好呀";
    }

}
